<?php

namespace app\user\controller;

use app\common\respond\HttpApi;
use app\user\logic\PigLogic;
use think\facade\Db;

class PigController
{
    /**
     * @Description 生猪列表
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/03/20 10:52:32
     * @return \think\response\Json
     */
    public function list(HttpApi $respond)
    {
        $param = [
            'page' => request()->get('page') ?: 1,
            'page_size' => request()->get('page_size') ?: 10,
            'fence_id' => request()->get('fence_id'),
            'barn_id' => request()->get('barn_id'),
            'ear_code' => request()->get('ear_code'),
            'status' => request()->get('status'),
            'max_weight' => request()->get('max_weight'),
            'min_weight' => request()->get('min_weight'),
            'in_time_start' => request()->get('in_time_start'),
            'in_time_end' => request()->get('in_time_end'),
            'out_time_start' => request()->get('out_time_start'),
            'out_time_end' => request()->get('out_time_end'),
            'record_time_start' => request()->get('record_time_start'),
            'record_time_end' => request()->get('record_time_end'),
        ];
        $rule = [
            'page' => '>:0',
            'page_size' => '>:0',
            'fence_id' => '>:0',
            'barn_id' => '>:0',
            'ear_code' => 'max:30',
            'status' => '>:0',
            'max_weight' => '>:0',
            'min_weight' => '>:0',
            'in_time_start' => 'dateFormat:Y-m-d H:i:s',
            'in_time_end' => 'dateFormat:Y-m-d H:i:s',
            'out_time_start' => 'dateFormat:Y-m-d H:i:s',
            'out_time_end' => 'dateFormat:Y-m-d H:i:s',
            'record_time_start' => 'dateFormat:Y-m-d H:i:s',
            'record_time_end' => 'dateFormat:Y-m-d H:i:s',
        ];
        validate($rule)->check($param);

        $logic = new PigLogic();
        $data = $logic->getList($param);

        return $respond->success($data);
    }

    /**
     * @Description 获取猪的详情
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/09/05 12:04:37
     * @return null
     */
    public function detail(HttpApi $respond)
    {
        $param = [
            'id' => request()->get('id'),
        ];
        $rule = [
            'id' => 'require|>:0'
        ];
        validate($rule)->check($param);

        $logic = new PigLogic();
        $data = $logic->getDetail($param['id']);

        return $respond->success($data);
    }

    /**
     * @Description 详情补充
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/09/20 11:36:05
     * @return null
     */
    public function detailSupply(HttpApi $respond)
    {
        $param = [
            'id' => request()->get('id'),
        ];
        $rule = [
            'id' => 'require|>:0'
        ];
        validate($rule)->check($param);

        $factoryId = request()->user_info['factory_id'];

        $logic = new PigLogic();
        $data = $logic->detailSupply($factoryId,$param['id']);

        return $respond->success($data);
    }

    /**
     * @Description 猪的下拉选择列表
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/09/11 18:28:01
     * @return null
     */
    public function selectList(HttpApi $respond)
    {
        $param = [
            'page' => (int)request()->get('page') ?: 1,
            'page_size' => request()->get('page_size') ?: 10,
            'fence_id' => request()->get('fence_id'),
            'ear_code' => request()->get('ear_code')
        ];
        $rule = [
            'page' => 'require|>:0',
            'page_size' => '>:0',
            'fence_id' => '>:0',
            'ear_code' => 'max:30'
        ];
        validate($rule)->check($param);

        $logic = new PigLogic();
        $data = $logic->selectList($param);

        return $respond->success($data);
    }

    public function inOut(HttpApi $respond)
    {
        $param = [
            'start_month' => request()->get('start_month'),
            'end_month' => request()->get('end_month'),
            'fence_id' => request()->get('fence_id')
        ];
        $rule = [
            'start_month' => 'require|dateFormat:Y-m',
            'end_month' => 'require|dateFormat:Y-m',
            'fence_id' => 'integer|>:0',
        ];
        validate($rule)->check($param);

        $factoryId = request()->user_info['factory_id'];
        $logic = new PigLogic();
        $data = $logic->inOut($factoryId, $param);

        return $respond->success($data);
    }

    /**
     * @Description 健康统计
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/03/20 16:50:41
     * @return \think\response\Json
     */
    public function healthStatistic(HttpApi $respond)
    {
        $param = [
            'barn_id' => request()->get('barn_id') ?: 0,
            'fence_id' => request()->get('fence_id') ?: 0
        ];
        $rule = [
            'barn_id' => 'integer|>=:0',
            'fence_id' => 'integer|>=:0'
        ];
        validate($rule)->check($param);

        $logic = new PigLogic();
        $data = $logic->healthStatistic($param);

        return $respond->success($data);
    }

    /**
     * @Description 食谱中心 -- 生猪列表
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/03/27 10:26:34
     * @return \think\response\Json
     */
    public function useCookBookPig(HttpApi $respond)
    {
        $param = [
            'page' => (int)request()->get('page') ?: 1,
            'page_size' => (int)request()->get('page_size') ?: 10,
            'barn_id' => request()->get('barn_id'),
            'fence_id' => request()->get('fence_id')
        ];
        $rule = [
            'page' => 'require|integer|>:0',
            'page_size' => 'require|integer|>:0',
            'barn_id' => 'integer|>:0',
            'fence_id' => 'integer|>:0',
        ];
        validate($rule)->check($param);

        $logic = new PigLogic();
        $data = $logic->useCookBookBarn($param);

        return $respond->success($data);
    }

}